package cn.edu.szh.view.ScoreInput;

import cn.edu.szh.bean.*;
import cn.edu.szh.dao.ScoreDao;
import cn.edu.szh.dao.ScoreInputDao;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class ScoreInputUI extends javax.swing.JFrame {
    List<ScoreInfo> scoreInfos;
    JTextField[][] textFields;
    JPanel  panelCenter = new JPanel();
    ScoreInputDao scoreInputDao  = new ScoreInputDao();
    public ScoreInputUI(GradeInfo gradeInfo, ClassInfo classInfo, SubjectInfo subjectInfo) {
        setTitle("录入成绩");
        ScoreDao  scoreDao = new ScoreDao();
        scoreInfos = scoreDao.getByGradeIdAndClassIdAndSubjectId(gradeInfo.getGrade_id(),classInfo.getClass_id(),subjectInfo.getSubject_id());
        if(scoreInfos.size()==0){
            setBounds(100,100,600,300);
            add(new JLabel("该班不存在学生"));
            return;
        }
        setLayout(new BorderLayout());
        JPanel  panelUP = new JPanel();
        JPanel  panelDown = new JPanel();


        setBounds(100,100,600,200+(scoreInfos.size()+2)*35);

        add(panelUP,BorderLayout.NORTH);
        add(panelCenter,BorderLayout.CENTER);
        add(panelDown,BorderLayout.SOUTH);
        panelUP.setPreferredSize(new Dimension(600,100));
        panelDown.setPreferredSize(new Dimension(600,100));

        panelCenter.setLayout(new GridLayout(scoreInfos.size()+1,7));
        addBlank(1);
        textFields  = new JTextField[scoreInfos.size()][2];
        for(int i=0;i<scoreInfos.size();i++){
            panelCenter.add(new JLabel(scoreInfos.get(i).getStudent_id()+scoreInfos.get(i).getName()));
            panelCenter.add(new JLabel("成绩"));
            panelCenter.add(textFields[i][0] = new JTextField());
            panelCenter.add(new JLabel("学期"));
            panelCenter.add(textFields[i][1] = new JTextField());
            addBlank(2);
        }
        addBlank(4);
        JButton  button = new JButton("提交");
        button.addActionListener(e -> {
            addData();
        });
        panelCenter.add(button);
        JButton  button1 = new JButton("取消");
        button1.addActionListener(e -> {
            this.dispose();
        });
        panelCenter.add(button1);
    }

    private void addData() {
        List<ScoreInputInfo> list = new ArrayList<>();
        for(ScoreInfo  scoreInfo: scoreInfos ){
            ScoreInputInfo  scoreInputInfo = new ScoreInputInfo();
            scoreInputInfo.setGrade_id(scoreInfo.getGrade_id());
            scoreInputInfo.setClass_id(scoreInfo.getClass_id());
            scoreInputInfo.setStudent_id(scoreInfo.getStudent_id());
            scoreInputInfo.setName(scoreInfo.getName());
            scoreInputInfo.setSubject_id(scoreInfo.getSubject_id());
            scoreInputInfo.setSname(scoreInfo.getSname());
            scoreInputInfo.setScore(Double.parseDouble(textFields[scoreInfos.indexOf(scoreInfo)][0].getText()));
            scoreInputInfo.setTerm(String.valueOf(Double.parseDouble(textFields[scoreInfos.indexOf(scoreInfo)][1].getText())));
            list.add(scoreInputInfo);
        }
        if ((scoreInputDao.add(list))){
            JOptionPane.showMessageDialog(this,"添加成功");
        }else {
            JOptionPane.showMessageDialog(this,"添加失败");
        }
        // masterUseDao.add(list);

    }

    private void addBlank(int num) {
        for (int i = 0; i < num; i++) {
            panelCenter.add(new JLabel(""));
        }
    }

}
